package cn.zjh.org.service.impl;

import cn.zjh.base.exception.BusinessException;
import cn.zjh.user.service.service.impl.BaseServiceImpl;
import cn.zjh.base.utils.BaiduAuditUtils;
import cn.zjh.base.utils.EmailUtil;
import cn.zjh.org.vo.ShopVo;
import cn.zjh.org.domain.Employee;
import cn.zjh.org.domain.Shop;
import cn.zjh.org.mapper.EmployeeMapper;
import cn.zjh.org.mapper.ShopMapper;
import cn.zjh.org.service.IShopService;
import cn.zjh.org.domain.ShopAuditLog;
import cn.zjh.org.mapper.ShopAuditLogMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zjh
 * @since 2022-07-01
 */


@Service
public class ShopServiceImpl extends BaseServiceImpl<Shop> implements IShopService {


    @Autowired
    private ShopMapper shopMapper;


    @Autowired
    private EmployeeMapper employeeMapper;


    @Autowired
    private ShopAuditLogMapper shopAuditLogMapper;

    @Autowired
    private EmailUtil emailUtil;



    /**
    * @Title: settlement
    * @Description: 店铺入驻业务层方法
    * @Author: Mr.Zhang
    * @Version: 1.0
    * @Date:  2022/7/1 20:31
    * @Parameters:
    * @Return
    */
    @Override
    public void settlement(Shop shop) {
        // 获取前端店铺注册用户对象信息
        Employee admin = shop.getAdmin();
        //1.判断必要参数是否为空
        //  判断管理员信息是否为空
        if(StringUtils.isEmpty(admin.getUsername())
                && StringUtils.isEmpty(admin.getPhone())
                && StringUtils.isEmpty(admin.getEmail())
                && StringUtils.isEmpty(admin.getPassword())){
            throw new BusinessException("用户信息不能为空，请仔细查看是否填写完成用户信息！");
        }

        //  判断店铺信息是否为空
        if(StringUtils.isEmpty(shop.getTel())
                && StringUtils.isEmpty(shop.getAddress())
                && StringUtils.isEmpty(shop.getName())){
            throw new BusinessException("店铺信息不能为空，请仔细查看是否填写完成店铺信息！");
        }

        // 2.判断密码是否一致
        if (!admin.getPassword().equals(admin.getComfirmPassword())){
            throw new BusinessException("密码不一致请重新填写！");
        }

        //  明天我们使用百度AI，还需要在此处审核店铺的某些信息是否合规
        Boolean shopNameTmp = BaiduAuditUtils.TextCensor(shop.getName());
        if (!shopNameTmp){
            throw new BusinessException("店铺名称不合规，请重新填写！");
        }

        //@TODO 百度地图智能定位
        Boolean shopAddressTmp = BaiduAuditUtils.TextCensor(shop.getAddress());
        if (!shopAddressTmp){
            throw new BusinessException("店铺地址不合规，请重新填写！");
        }

        // 图片审核前端传递的商标是否合规
        String imgs = "http://123.207.27.208"+shop.getLogo();
        boolean shopLogoTmp = BaiduAuditUtils.ImgCensor(imgs);
        if (!shopLogoTmp){
            throw new BusinessException("店铺logo不合规，请上传！");
        }


        // 3.根据店铺入驻的店铺名称查询店铺是否存在
        Shop loadShopTmp = shopMapper.loadByName(shop.getName());
        if (loadShopTmp != null){
            throw new BusinessException("该店铺已经注册，请重新更换店铺信息!");
        }


        // 4.查询店长是否已经注册并绑定过某个店铺，如果是则不符合我们业务要求
        Employee empTmp = employeeMapper.loadByUserName(admin.getUsername());
        if(empTmp != null && empTmp.getShopId() != null){
            throw new BusinessException("该用户已经绑定店铺，请直接登录！");
        }

        // 此处你们可以自己更改为根据主键查询店铺的admin_id
        // 5.如果店长没有注册，那么注册店长
        if (empTmp == null){
            employeeMapper.save(admin);
            empTmp = admin;
        }

        // 6.此时员工已经绝对注册了店长，那么就将店长的主键设置到店铺的外键
        shop.setAdminId(empTmp.getId());
        // 6.2.保存店铺,一定要返回主键
        shopMapper.save(shop);
        // 6.3.将店铺的主键设置到店长的外键
        empTmp.setShopId(shop.getId());
        // 6.4.修改员工(店长)数据
        employeeMapper.update(empTmp);

    }

    /**
    * @Title: pass
    * @Description: 通过
    * @Author: Mr.Zhang
    * @Version: 1.0
    * @Date:  2022/7/2 19:24
    * @Parameters:
    * @Return
    */
    @Override
    public void pass(ShopAuditLog shopAuditLog) {
        //获取shop_id
        Long shopId = shopAuditLog.getShopId();
        //根据ShopAuditLog日志中的shop_id将店铺状态修改为通过
        shopMapper.uptPassShopId(shopId);
        shopAuditLogMapper.save(shopAuditLog);
        emailUtil.sendPassEmail(shopAuditLog);

    }


    /**
    * @Title: reject
    * @Description: 驳回
    * @Author: Mr.Zhang
    * @Version: 1.0
    * @Date:  2022/7/2 19:24
    * @Parameters:
    * @Return
    */
    @Override
    public void reject(ShopAuditLog shopAuditLog) {
        //获取shop_id
        Long shopId = shopAuditLog.getShopId();
        //根据ShopAuditLog日志中的shop_id将店铺状态修改为未激活
        shopMapper.uptRejectShopId(shopId);
        shopAuditLogMapper.save(shopAuditLog);
        emailUtil.sendRejectEmail(shopAuditLog);

    }


    /**
    * @Title: cancel
    * @Description: 拒绝
    * @Author: Mr.Zhang
    * @Version: 1.0
    * @Date:  2022/7/3 20:19
    * @Parameters:
    * @Return
    */
    @Override

    public void cancel(ShopAuditLog shopAuditLog) {
        //修改店铺信息为未激活@TODO
        //根据shop_id查询出店铺，再将店铺状态修改为未激活
        //因为没做登录，将审核员的id写死
        shopAuditLog.setAuditId(325L);
        shopAuditLogMapper.save(shopAuditLog);
        emailUtil.sendCancelEmail(shopAuditLog);
    }


    /**
    * @Title: loadEchartsData
    * @Description: 查询报表信息
    * @Author: Mr.Zhang
    * @Version: 1.0
    * @Date:  2022/7/2 21:24
    * @Parameters:
    * @Return
    */
    @Override
    public Map<String, Object> loadEchartsData() {
        //获取报表数据
        List<ShopVo> shopVos = shopMapper.loadEchartsData();


        //状态
        List<Integer> x = shopVos.stream().map(ShopVo::getState).collect(Collectors.toList());



        //此状态的数量
        List<Integer> y = shopVos.stream().map(ShopVo::getCount).collect(Collectors.toList());





        Map<String,Object> data = new HashMap<>();
        data.put("x",x);
        data.put("y",y);

        return data;
    }

    @Override
    public List<ShopAuditLog> logForm(Long id) {
        return shopMapper.logForm(id);
    }
}
